package com.cgi.ndk.wf.pack;

import com.cgi.ndk.wf.scan.Scan;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Link;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;

/**
 *
 * @author brizat
 */
@Component
public class PackageResourceAssemblerSupport extends ResourceAssemblerSupport<Package, PackageResource> {

    @Autowired
    private EntityLinks entityLinks;
    
    public PackageResourceAssemblerSupport() {
        super(PackageController.class, PackageResource.class);
    }

    @Override
    public PackageResource toResource(Package entity) {
        PackageResource resource = instantiateResource(entity);
        resource.setPack(entity);
        resource.add(linkTo(PackageController.class).slash(entity.getMid()).withSelfRel());

        if (entity.getScans() != null) {
            List<Link> links = new ArrayList<>();
            entity.getScans().stream().forEach(scan -> {
                links.add(entityLinks.linkToSingleResource(Scan.class, scan).withRel("scan"));
                
            });
            resource.add(links);
        }
        return resource;
    }

}
